package io.renren.modules.app.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.zheok.testing.entity.*;
import com.zheok.testing.param.SearchParam;
import com.zheok.testing.vo.LableJsonVo;
import com.zheok.testing.vo.LableVo;
import com.zheok.testing.vo.ParamJsonVo;
import io.renren.common.utils.ShiroUtils;
import io.renren.modules.app.service.CatagoryService;
import io.renren.modules.app.service.ProductService;
import io.renren.modules.app.service.ShopService;
import io.renren.modules.app.service.TranslateUtil;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import com.zheok.testing.dao.ProductDao;
import org.springframework.util.StringUtils;


@Service("productService")
public class ProductServiceImpl extends ServiceImpl<ProductDao, ProductEntity> implements ProductService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<ProductEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        String username = userEntity.getUsername();
        if (!"admin".equals(username)) {
            wrapper.lambda().eq(ProductEntity::getShopId, userEntity.getUserId());
        }
        if (!StringUtils.isEmpty(key)) {
            wrapper.lambda().like(ProductEntity::getProductName, key);
        }
        IPage<ProductEntity> page = this.page(
                new Query<ProductEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Autowired
    private ShopService shopService;
    @Autowired
    private CatagoryService catagoryService;

    @Override
    public void saveinfo(ProductEntity product) {

        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        product.setShopId(userEntity.getUserId());
        product.setShopName(userEntity.getName());
        //分类名称
        String catagoryId = product.getCatagoryId();
        String[] split = catagoryId.split(",");
        CatagoryEntity byId1 = catagoryService.getById(split[2]);
        product.setCatagoryName(byId1.getCatagoryName());


        this.saveOrUpdate(product);
    }


    @Override
    public List<LableVo> getCatagory2(String catagory) {
        CatagoryEntity byId = catagoryService.getById(catagory);

        String label = byId.getLabel();//这是一个Json

        JSONObject jsonObject = JSON.parseObject(label);
//        List<LableVo> lableVos = JSON.parseObject(label, new TypeReference<List<LableVo>>() {
//        });
        JSONArray headers1 = jsonObject.getJSONArray("headers");

        String s = JSON.toJSONString(headers1);

        List<LableJsonVo> lableJsonVos = JSON.parseObject(s, new TypeReference<List<LableJsonVo>>() {
        });

        List<LableVo> collect = lableJsonVos.stream().map(item -> {
            LableVo lableVo = new LableVo();
            lableVo.setLabel(item.getTitle());
            lableVo.setValue(item.getTitle());
            return lableVo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<ParamJsonVo> getParam(String productId) {
        ProductEntity byId = this.getById(productId);

        String label = byId.getProductParam();//这是一个Json

        JSONObject jsonObject = JSON.parseObject(label);
//        List<LableVo> lableVos = JSON.parseObject(label, new TypeReference<List<LableVo>>() {
//        });
        JSONArray headers1 = jsonObject.getJSONArray("headers");

        String s = JSON.toJSONString(headers1);

        List<ParamJsonVo> lableJsonVos = JSON.parseObject(s, new TypeReference<List<ParamJsonVo>>() {
        });
        return lableJsonVos;
    }

    @Override
    public List<ProductEntity> search(SearchParam params) {
        QueryWrapper<ProductEntity> wrapper = new QueryWrapper<>();
        if (!Strings.isEmpty(params.getProductLable())) {
            wrapper.lambda().like(ProductEntity::getProductLable, params.getProductLable());
        }
        if (!Strings.isEmpty(params.getKeywords())) {
            wrapper.lambda().like(ProductEntity::getProductName, params.getKeywords());
        }
        if (!Strings.isEmpty(params.getShopId())) {
            wrapper.lambda().eq(ProductEntity::getShopId, params.getShopId());
        }
        if (!Strings.isEmpty(params.getCatagoryId())) {
            wrapper.lambda().like(ProductEntity::getCatagoryId, params.getCatagoryId());
        }
        if (!Strings.isEmpty(params.getCatagory2())) {
            wrapper.lambda().like(ProductEntity::getCatagoryId, params.getCatagory2());
        }
        return this.list(wrapper);
    }
}